<template>
  <div>
    <div class="root">
      <van-nav-bar left-arrow @click-left="$router.back()">
        <template #title>
          <div>
            {{ housePage.community }}
          </div>
        </template>
      </van-nav-bar>
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item
          v-for="(item, index) in housePage.houseImg"
          :key="index"
        >
          <img
            :src="`http://liufusong.top:8080${item}`"
            alt=""
            @click="imgShowFn(index)"
          />
        </van-swipe-item>
      </van-swipe>
      <div class="main">
        <h3 class="title">{{ housePage.title }}</h3>
        <div>
          <van-tag
            size="medium"
            color="#e1f5f8"
            text-color="#39becd"
            v-for="(item, index) in housePage.tags"
            :key="index"
            >{{ item }}</van-tag
          >
        </div>
        <van-row>
          <van-col span="8">
            <div>{{ housePage.price }}</div>
            <div>租金</div>
          </van-col>
          <van-col span="8">
            <div>{{ housePage.roomType }}</div>
            <div>房型</div>
          </van-col>
          <van-col span="8">
            <div>{{ housePage.size }}</div>
            <div>面积</div>
          </van-col>
        </van-row>
        <van-row>
          <van-col span="12">
            <div><span>装修：</span><span>精装</span></div>
            <div>
              <span>楼层：</span><span>{{ housePage.floor }}</span>
            </div>
          </van-col>
          <van-col span="12">
            <div>
              <span>楼层：</span
              ><span v-for="(item, index) in housePage.oriented" :key="index">{{
                item
              }}</span>
            </div>
            <div><span>类型：</span><span>普通住宅</span></div>
          </van-col>
        </van-row>
      </div>
      <div class="house_map">
        <p>小区：天山星城</p>
        <div style="height: 145px;font-size: 15px;text-align: center;line-height: 145px;" v-if="isMapShow">地图</div>
        <baidu-map
        v-else
          class="map"
          :zoom="15"
          :center="{
            lng: housePage.coord.longitude,
            lat: housePage.coord.latitude,
          }"
        >
          <bm-label
            :content="housePage.community"
            :position="{
              lng: housePage.coord.longitude,
              lat: housePage.coord.latitude,
            }"
            :labelStyle="{
              backgroundColor: ' rgb(238, 93, 91)',
              color: 'rgb(255, 255, 255)',
              padding: '5px 10px',
            }"
            title="Hover me"
          />
        </baidu-map>
      </div>
      <div class="house_about">
        <div>房屋配套</div>
        <p v-if="isMapShow">暂无配套</p>
        <span v-else>{{housePage.supporting.join("")
        }}</span>
      </div>
      <div class="house_info">
        <div class="title">房源概况</div>
        <div class="info">
          <div class="user">
            <div>
              <img src="http://liufusong.top:8080/img/avatar.png" alt="头像" />
            </div>
            <div class="user_info">
              <div>王女士</div>
              <div class="user_Auth">
                <van-icon name="shield-o" />
                已认证房主
              </div>
            </div>
          </div>
          <span>发送消息</span>
        </div>
        <div class="text">
          1.周边配套齐全，地铁四号线陶然亭站，交通便利，公交云集，距离北京南站、西站都很近距离。
          2.小区规模大，配套全年，幼儿园，体育场，游泳馆，养老院，小学。
          3.人车分流，环境优美。
          4.精装两居室，居家生活方便，还有一个小书房，看房随时联系。
        </div>
      </div>
      <div class="house_recommend">
        <div class="title">猜你喜欢</div>
      </div>
      <van-tabbar v-model="active">
        <van-tabbar-item @click="colleFn(isShow)">
          <van-icon name="star" color="#fb4e0d" v-if="isShow" /><van-icon
            name="star-o"
            v-else
          />
          收藏</van-tabbar-item
        >
        <van-tabbar-item>在线咨询</van-tabbar-item>
        <van-tabbar-item @click="telFn">电话预约</van-tabbar-item>
      </van-tabbar>
    </div>
  </div>
</template>

<script>
import { Toast, ImagePreview, Dialog } from 'vant'
import {
  houseMsgApi,
  favoritesApi,
  favoritesHouseApi,
  favoritesDelApi
} from '@/api'
import store from '../../store'

export default {
  data () {
    return {
      housePage: {},
      active: 0,
      isShow: true,
      isMapShow: true
    }
  },
  async created () {
    console.log(this.$route.params.keyword)
    const id = this.$route.params.keyword
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true
    })
    const res = await houseMsgApi({ id })
    // console.log(res2.data.body.isFavorite)
    this.housePage = res.data.body
    const res2 = await favoritesHouseApi({ id: this.housePage.houseCode })
    console.log(res2)
    this.isShow = res2.data.body.isFavorite
    // console.log(this.housePage)
    this.isMapShow = false
    Toast.clear()
  },
  methods: {
    async colleFn (bool) {
      if (!store.state.token) {
        Dialog.confirm({
          title: '好客租房',
          message: '你还未登录，是否跳转登陆页面',
          confirmButtonText: '跳转'
        })
          .then(() => {
            // on confirm
            this.$router.replace('/login')
          })
          .catch(() => {
            // on cancel
          })
        return
      }
      console.log(bool)
      this.isShow = !this.isShow
      if (bool) {
        const res = await favoritesDelApi({ id: this.housePage.houseCode })
        console.log(res)
      } else {
        const res = await favoritesApi({ id: this.housePage.houseCode })
        console.log(res)
      }
      // 收藏接口
    },
    imgShowFn (index) {
      // console.log(this.housePage.oriented)

      const img = this.housePage.houseImg.map(
        (item) => `http://liufusong.top:8080${item}`
      )

      ImagePreview({
        images: img,
        startPosition: index,
        closeable: true
      })
    },
    telFn () {
      window.location.href = 'tel://400-618-4000'
    }
  }
}
</script>

<style lang="less" scoped>
.root {
  height: 100%;
  background-color: #f6f5f6;
  padding-bottom: 50px;
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #f6f5f6;
  height: 252px;
  img {
    width: 100%;
  }
}
/deep/.van-swipe__indicator {
  width: 8px;
  height: 8px;
  background-color: #cccccc;
}
.title {
  font-weight: 400;
  font-size: 16px;
  color: #333;
  height: 40px;
  margin: 16px 0;
}
.main {
  padding: 15px;
  background-color: #fff;
}
.van-col--8 {
  border-top: 1px solid #cecece;
  border-bottom: 1px solid #cecece;
  margin: 15px 0;
  padding: 15px 0;
  text-align: center;
  height: 80px;
  div {
    height: 26px;
    // background-color: pink;
    font-size: 14px;
    color: #999;
  }
  & div:first-child {
    font-size: 18px;
    font-weight: 700;
    color: #f75743;
  }
}
.van-col--12 {
  div {
    height: 26px;
    span {
      font-size: 13px;
      &:first-child {
        color: #999;
        padding-right: 5px;
      }
    }
  }
}
.house_map {
  margin-top: 10px;
  background-color: #fff;
  p {
    padding: 0 15px;
    font-size: 14px;
    height: 44px;
    line-height: 44px;
  }
}

.map {
  width: 100%;
  height: 145px;
}
.sample {
  width: 120px;
  height: 40px;
  line-height: 40px;
  background: rgba(0, 0, 0, 0.5);
  overflow: hidden;
  box-shadow: 0 0 5px #000;
  color: #fff;
  text-align: center;
  padding: 10px;
  position: absolute;
}
.sample.active {
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
}
.house_about {
  padding: 0 10px;
  margin-bottom: 10px;
  background-color: #fff;
  div {
    font-weight: 600;
    font-size: 15px;
    margin-bottom: 10px;
    padding: 15px 0;
    border-bottom: 1px solid #cecece;
  }
  span {
    font-size: 12px;
  }
}
.van-tabbar-item {
  border-top: 1px solid #cecece;
  color: #999;
  font-size: 17px;
  &:nth-child(2) {
    border: 1px solid #cecece;
    border-bottom: none;
  }
  &:last-child {
    background-color: #21b97a;
    color: #fff;
  }
}
.house_info {
  margin: 10px 0;
  padding: 0 10px;
  line-height: 150%;
  background: #fff;
  .title {
    font-weight: 600;
    height: 52px;
    font-size: 15px;
    margin-bottom: 10px;
    padding: 15px 0;
    border-bottom: 1px solid #cecece;
  }
  .info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user {
      display: flex;
      img {
        width: 52px;
        height: 52px;
        margin: 10px 10px 0 0;
      }
      .user_info {
        padding: 15px 0 0 5px;
        font-size: 14px;
        div {
          height: 21px;
        }
        .user_Auth {
          font-size: 12px;
          color: #fa5741;
          display: block;
        }
      }
    }
    span {
      color: #33be85;
      border: 1px solid #33be85;
      border-radius: 3px;
      text-align: center;
      width: 74px;
      height: 29px;
      line-height: 29px;
      font-size: 14px;
    }
  }
  .text {
    line-height: 1.6;
    padding: 10px 0;
    color: #333;
    font-size: 14px;
  }
}
.house_recommend {
  margin: 10px 0 0;
  padding: 0 15px;
  background: #fff;
  min-height: 100px;
  .title {
    font-weight: 600;
    font-size: 15px;
    margin-bottom: 10px;
    padding: 15px 0;
    border-bottom: 1px solid #cecece;
  }
}
</style>
